package com.lhwl.it.sbfl.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lhwl.it.sbfl.service.SbflService;
import com.lhwl.it.util.PageBean;
import com.lhwl.it.util.RequestMap;

@Controller
@RequestMapping("/itmanager/sbfl")
public class SbflAction {
	
	private Log log=LogFactory.getLog(this.getClass());
	
	@Resource
	private SbflService sbflService;
	@RequestMapping()
	public String index(HttpServletRequest request,HttpServletResponse response) throws Exception{
		return "/WEB-INF/jsp/sbfl/sbflgl";
	}
	
	
	@RequestMapping(value="/json",method=RequestMethod.POST)
	public String sbflJson(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap map=new RequestMap(request);
		PageBean pageBean=this.sbflService.getPage("SbflMapper.listAll", map);
		List list=pageBean.getList();
		request.setAttribute("smap", map);
		JSONArray jsonArray=JSONArray.fromObject(list);
		JSONObject json=new JSONObject();
		json.put("total", pageBean.getRowscount());
		json.put("rows", jsonArray);
		response.getWriter().print(json);
		return null;
	}
	
	@RequestMapping(value="/all",method=RequestMethod.GET)
	public String json(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap map=new RequestMap(request);
		List list=this.sbflService.list("SbflMapper.listAll", map);
		request.setAttribute("smap", map);
		JSONArray jsonArray=JSONArray.fromObject(list);
		response.getWriter().print(jsonArray);
		return null;
	}
	
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap map=new RequestMap(request);
		try{
			this.sbflService.insert("SbflMapper.insert", map);
			response.getWriter().print("success");
		}catch(Exception e){
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public String edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap map=new RequestMap(request);
		try{
			this.sbflService.update("SbflMapper.edit", map);
			response.getWriter().print("success");
		}catch(Exception e){
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String deleteCl(HttpServletRequest request,HttpServletResponse response)throws Exception {
		HashMap map=new RequestMap(request);
		String id= map.get("ids").toString();
		String[] ids=id.split(",");
		try{
			for(int i=0;i<ids.length;i++){
				map.remove("id");
				map.put("id", ids[i]);
				this.sbflService.delete("SbflMapper.delete", map);
			}
			response.getWriter().print("success");
		}catch(Exception e){
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
}
